// FileUtility.cpp: implementation of the CFileUtility class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileUtility.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFileUtility::CFileUtility()
{

}

CFileUtility::~CFileUtility()
{

}

BOOL CFileUtility::FileExists(LPCTSTR lpFileName)
{
    BOOL retval = FALSE;

    DWORD dwAttrs = ::GetFileAttributes(lpFileName);
    if (dwAttrs != 0xFFFFFFFF && !(dwAttrs & FILE_ATTRIBUTE_DIRECTORY))
        retval = TRUE;

    return retval;
}

BOOL CFileUtility::DirectoryExists(LPCTSTR lpPathName)
{
    BOOL retval = FALSE;

    DWORD dwAttrs = ::GetFileAttributes(lpPathName);
    if (dwAttrs != 0xFFFFFFFF && (dwAttrs & FILE_ATTRIBUTE_DIRECTORY))
        retval = TRUE;

    return retval;
}

HRESULT CFileUtility::GetFileSize(LPCTSTR lpFileName, UINT64* lpiSize)
{
// 	LARGE_INTEGER a;

    DWORD dwLow, dwHigh, dwError;
    HANDLE hFile = CreateFile(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
        return E_FAIL;

    dwLow = ::GetFileSize(hFile, &dwHigh);
    if (dwLow == 0xFFFFFFFF && (dwError = GetLastError()) != NO_ERROR)
    {
        CloseHandle(hFile);
        return E_FAIL;
    }

    *lpiSize = dwHigh;
    *lpiSize = *lpiSize << 32;
    *lpiSize |= dwLow;

    CloseHandle(hFile);
    return S_OK;
}
